package tt.dz.egobus.servicemanager.vo;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import tt.dz.egobus.servicemanager.bean.EntityViewVo;
import tt.dz.egobus.servicemanager.entities.Bus;
import tt.dz.egobus.servicemanager.entities.BusType;

/**
 * @author Anson
 * @date 17-4-14
 * @name bus的vo
 * @since 1.0.4
 */
public class BusViewVo implements EntityViewVo<Bus, BusViewVo> {
    private static final long serialVersionUID = 6333941401294484288L;

    private static final Logger LOGGER = LoggerFactory.getLogger(BusViewVo.class);


    @Override
    public Bus convert() {
        Bus pojo = new Bus();
        try {
            BeanUtils.copyProperties(this, pojo);
        } catch (BeansException e) {
            LOGGER.error("BusViewVo -> Bus bean拷贝异常, viewVo={}", this, e);
        }
        return pojo;
    }

    @Override
    public BusViewVo view(Bus pojo) {
        if (pojo == null) {
            return this;
        }
        try {
            BeanUtils.copyProperties(pojo, this);
            BusType busType = pojo.getBusType();
            if (busType != null) {
                this.setBusTypeViewVo(new BusTypeViewVo().view(busType));
            }

        } catch (BeansException e) {
            LOGGER.error("Bus  -> BusViewVo bean拷贝异常, viewVo={}", this, e);
        }
        return this;
    }


    private String id;
    /**
     * 车牌
     */
    @NotBlank(message = "车牌不能为空")
    @Length(max = 10)
    private String carNo;

    /**
     * 车辆状态
     * 车辆状态 0未激活，1：未使用,2：已经使用，3：异常，4：维修，-1：报废
     */
    private int status = 0;

    /**
     * 当前关联的车型
     */
    @NotBlank(message = "车牌不能为空")
    private String busTypeId;

    private BusTypeViewVo busTypeViewVo;

    public String getCarNo() {
        return carNo;
    }

    public void setCarNo(String carNo) {
        this.carNo = carNo;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getBusTypeId() {
        return busTypeId;
    }

    public void setBusTypeId(String busTypeId) {
        this.busTypeId = busTypeId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public BusTypeViewVo getBusTypeViewVo() {
        return busTypeViewVo;
    }

    public void setBusTypeViewVo(BusTypeViewVo busTypeViewVo) {
        this.busTypeViewVo = busTypeViewVo;
    }

    @Override
    public String toString() {
        return "BusViewVo{" +
                "id='" + id + '\'' +
                ", carNo='" + carNo + '\'' +
                ", status=" + status +
                ", busTypeId='" + busTypeId + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BusViewVo)) return false;

        BusViewVo busViewVo = (BusViewVo) o;

        if (getStatus() != busViewVo.getStatus()) return false;
        if (getId() != null ? !getId().equals(busViewVo.getId()) : busViewVo.getId() != null) return false;
        if (getCarNo() != null ? !getCarNo().equals(busViewVo.getCarNo()) : busViewVo.getCarNo() != null) return false;
        return getBusTypeId() != null ? getBusTypeId().equals(busViewVo.getBusTypeId()) : busViewVo.getBusTypeId() == null;

    }

    @Override
    public int hashCode() {
        int result = getId() != null ? getId().hashCode() : 0;
        result = 31 * result + (getCarNo() != null ? getCarNo().hashCode() : 0);
        result = 31 * result + getStatus();
        result = 31 * result + (getBusTypeId() != null ? getBusTypeId().hashCode() : 0);
        return result;
    }
}
